"""
@author: xiangping
@contact: xiangpingbu@gmail.com
@time: 2020/1/10 4:37 下午
@file: shaojie_controller
@Desc:
"""
from datetime import datetime, timedelta
from typing import List

from app.database import convert
from app.models.po.lugang.lugang_original_po import LuGangActivinessPredictPO

from flask import Blueprint, request, current_app
from app.controllers.response.response import SuccessResult
from app.exts import ironman_redis as redis
import logging
from sqlalchemy import and_

from app.utils.date_util import DailyDateIterator

bp = Blueprint('lugang_long_activiness', __name__, url_prefix="/api/v1/lugang/act")


@bp.route('/data', methods=['GET'])
def mock_lugang_activiness_data():
    import random

    for item in [['CG_LT_GL_GL04_LGWDBG10500TE1247', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG11500TE1249', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG11500TE1251', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG10500TE1245', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG11500TE1255', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG9500TE1235', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG10500TE1237', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG10500TE1241', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG9500TE1233', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG10500TE1243', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8500TE1223', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG9500TE1225', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG9500TE1229', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG9500TE1227', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8500TE1221', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG9500TE1231', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8000TE1207', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8000TE1205', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8500TE1211', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8500TE1209', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8500TE1217', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8500TE1213', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8500TE1219', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7500TE1191', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7500TE1190', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8000TE1195', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8000TE1193', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8000TE1199', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8000TE1201', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG8000TE1203', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7000TE1175', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7000TE1173', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7500TE1179', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7500TE1177', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7500TE1185', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7500TE1181', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7500TE1187', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7000TE1161', 200, 600],
                 ['CG_LT_GL_GL04_LDWDBG6500TE1159', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7000TE1165', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7000TE1163', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7000TE1169', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7000TE1167', 200, 600],
                 ['CG_LT_GL_GL04_LDWDBG6500TE1157', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG7000TE1171', 200, 600],
                 ['CG_LT_GL_GL04_LDWDBG6200TE1134', 200, 600],
                 ['CG_LT_GL_GL04_LDWDBG6200TE1137', 200, 600],
                 ['CG_LT_GL_GL04_LDWDBG6200TE1140', 200, 600],
                 ['CG_LT_GL_GL04_LDWDBG6200TE1144', 200, 600],
                 ['CG_LT_GL_GL04_LDWDBG5700TE1109', 200, 600],
                 ['CG_LT_GL_GL04_LDWDBG5700TE1112', 200, 600],
                 ['CG_LT_GL_GL04_LDWDBG5700TE1115', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG10500TE1246', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG9500TE1234', 200, 600],
                 ['CG_LT_GL_GL04_LDWDBG6200TE1135', 200, 600],
                 ['CG_LT_GL_GL04_LGWDBG6500TE1159', 200, 600],
                 ['CG_LT_GL_GL04_LJWDBG4000TE1101', 200, 600],
                 ['CG_LT_GL_GL04_LDWDBG5700TE1119', 200, 600], ]:
        key = item[0]
        redis.hset("lugang_activiness_long", key, random.randint(item[1], item[2]))
        redis.hset("lugang_activiness_long_update", key, datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S"))
        logging.info(key)
    return SuccessResult(detail="ok")


@bp.route('/longTerm', methods=['GET'])
def lugang_activiness_long():
    """
    炉缸长期趋势预警
    :return:
    """
    start = request.args.get("start")
    end = request.args.get("end")

    start = datetime.strptime(start, "%Y-%m-%d %H:%M:%S")
    end = datetime.strptime(end, "%Y-%m-%d %H:%M:%S")
    records: List[LuGangActivinessPredictPO] = convert.query(LuGangActivinessPredictPO,
                                                             and_(LuGangActivinessPredictPO.date_time >= start,
                                                             LuGangActivinessPredictPO.date_time <= end),
                                                             LuGangActivinessPredictPO.level == 'day',
                                                             LuGangActivinessPredictPO.type == 'longTerm').all()

    # records: List[LuGangActivinessPredictPO] = []
    if len(records) > 0:
        current = [record.current for record in records]
        current.append(None)
        predict = [record.predict for record in records]

        perv_id = records[0].id
        prev = convert.query(LuGangActivinessPredictPO,
                      and_(LuGangActivinessPredictPO.id < perv_id,
                      LuGangActivinessPredictPO.level == 'day',
                      LuGangActivinessPredictPO.type == 'longTerm'),order_by=LuGangActivinessPredictPO.id.desc()).first()

        if prev:
            predict.insert(0, prev.predict)
        else:
            predict.insert(0, None)

        next_day = records[len(records) - 1].date_time + timedelta(days=1)
        date = [record.date_time for record in records]
        date.append(next_day)
        date = [d.strftime("%Y-%m-%d") for d in date]

        return SuccessResult(detail=dict(current=current, predict=predict, date=date))
    return SuccessResult(detail=[])
